﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using YQGL.Common;

namespace YQGL.Controllers
{
    public class InfoPubController : EnterpriseBaseController
    {
        BLL.DM_QYDL DLBLL = new BLL.DM_QYDL();
        BLL.DM_QYLX LXBLL = new BLL.DM_QYLX();
        BLL.DM_QYLXMX MXBLL = new BLL.DM_QYLXMX();
        BLL.dm_SDYSX SXBLL = new BLL.dm_SDYSX();
        BLL.dm_SDYSX1 SX1BLL = new BLL.dm_SDYSX1();
        BLL.dm_SDYSX2 SX2BLL = new BLL.dm_SDYSX2();
        BLL.dm_SDYSX3 SX3BLL = new BLL.dm_SDYSX3();
        BLL.dm_SDYSX4 SX4BLL = new BLL.dm_SDYSX4();
        BLL.dm_SDYSX5 SX5BLL = new BLL.dm_SDYSX5();
        BLL.DJ_QYZCXX DJ_QYZCXXBLL = new BLL.DJ_QYZCXX();

        //
        // GET: /InfoPub/

        public ActionResult Index()
        {

            List<SelectListItem> DLList = DLBLL.GetModelList("").Select(h => new SelectListItem() { Text = h.qydl_mc, Value = h.qydl_dm }).ToList();

            List<SelectListItem> LXList = new List<SelectListItem>();

            List<SelectListItem> MXList = new List<SelectListItem>();



            List<SelectListItem> SXList = SXBLL.GetModelList("").Where(h=>h.XYBZ&h.Yxbz).Select(h => new SelectListItem() { Text = h.SDYSX_MC, Value = h.SDYSX_dm }).ToList();
            ;
            List<SelectListItem> SX1List = SX1BLL.GetModelList("").Select(h => new SelectListItem() { Text = h.SDYSX1_MC, Value = h.SDYSX1_dm }).ToList();

            List<SelectListItem> SX2List = SX2BLL.GetModelList("").Select(h => new SelectListItem() { Text = h.SDYSX2_MC, Value = h.SDYSX2_dm }).ToList();

            List<SelectListItem> SX3List = SX3BLL.GetModelList("").Select(h => new SelectListItem() { Text = h.SDYSX3_MC, Value = h.SDYSX3_dm }).ToList();

            List<SelectListItem> SX4List = SX4BLL.GetModelList("").Select(h => new SelectListItem() { Text = h.SDYSX4_MC, Value = h.SDYSX4_dm }).ToList();

            List<SelectListItem> SX5List = SX5BLL.GetModelList("").Select(h => new SelectListItem() { Text = h.SDYSX5_MC, Value = h.SDYSX5_dm }).ToList();



            Model.DJ_QYZCXX infoModel = new Model.DJ_QYZCXX();
            string actionOP = BaseStruct.ActionOperationStruct.ADD_NEXT;
            string LoginID = OperateSession.get("LoginID");
            if (string.IsNullOrEmpty(LoginID))
            {

                Response.Redirect(Url.Action("Index", "Index"));
            }
            else
            {
                ViewData["LoginID"] = LoginID;
                infoModel = DJ_QYZCXXBLL.GetModel(LoginID);

                if (infoModel != null)
                {
                    //if (infoModel.ZTDM == "2")
                    //{


                    //    return RedirectToAction("Index", "QYBB");
                    //}
                    actionOP = BaseStruct.ActionOperationStruct.EDIT;
                    if (!string.IsNullOrEmpty(infoModel.QYDL_DM))
                    {
                        LXList = LXBLL.GetModelList("qydl_dm='" + infoModel.QYDL_DM + "'").Select(h => new SelectListItem() { Text = h.QYLX_MC, Value = h.QYLX_DM }).ToList();
                    }

                    DLList.ForEach(h => h.Selected = h.Value == infoModel.QYDL_DM);
                    LXList.ForEach(h => h.Selected = h.Value == infoModel.QYLX_DM);
                    if (!string.IsNullOrEmpty(infoModel.QYLX_DM))
                    {
                        MXList = MXBLL.GetModelList("QYLX_DM='" + infoModel.QYLX_DM + "'").Select(h => new SelectListItem() { Text = h.QYLXMX_MC, Value = h.QYLXMX_DM }).ToList();


                        MXList.ForEach(h => h.Selected = infoModel.QYLXMX_DM.Contains(h.Value));

                    }
                    SX1List.ForEach(h => h.Selected = h.Value == infoModel.SDYSX1_DM);
                    SX2List.ForEach(h => h.Selected = h.Value == infoModel.SDYSX2_DM);

                    SX3List.ForEach(h => h.Selected = h.Value == infoModel.SDYSX3_DM);
                    SX4List.ForEach(h => h.Selected = h.Value == infoModel.SDYSX4_DM);
                    SX5List.ForEach(h => h.Selected = h.Value == infoModel.SDYSX5_DM);
                }
                else
                {

                    infoModel = new Model.DJ_QYZCXX();
                }


            }
            ViewData["DLList"] = DLList;

            ViewData["LXList"] = LXList;

            ViewData["MXList"] = MXList;
            ViewData["SXList"] = SXList;
            ViewData["SX1List"] = SX1List;
            ViewData["SX2List"] = SX2List;
            ViewData["SX3List"] = SX3List;
            ViewData["SX4List"] = SX4List;
            ViewData["SX5List"] = SX5List;
            ViewData["hdMethod"] = actionOP.ToString();
            return View(infoModel);
        }
        public string DlQuery(string value)
        {
            List<SelectListItem> LXList = LXBLL.GetModelList("qydl_dm='" + value + "'").Select(h => new SelectListItem() { Text = h.QYLX_MC, Value = h.QYLX_DM }).ToList();
            return JsonConvert.SerializeObject(LXList);


        }

        public string LXQuery(string value)
        {

            List<SelectListItem> MXList = MXBLL.GetModelList("QYLX_DM='" + value + "'").Select(h => new SelectListItem() { Text = h.QYLXMX_MC, Value = h.QYLXMX_DM }).ToList();
            return JsonConvert.SerializeObject(MXList);
        }

        public string Save()
        {


            bool isSuccess = true;
            string errorMessage = "";
            try
            {
                Model.DJ_QYZCXX infoModel = new Model.DJ_QYZCXX();
                infoModel.Qybm = OperateSession.get("LoginID");

                string Nsrmc = Fetch.Post("Nsrmc");

                if (string.IsNullOrEmpty(Nsrmc))
                {
                    isSuccess = false;
                    errorMessage = "企业名称不能为空";
                }
                else
                {


                    infoModel.Nsrmc = Nsrmc;
                }

                infoModel.ZZDM = Fetch.Post("ZZDM");
                infoModel.SCJYDZ = Fetch.Post("SCJYDZ");

                string JYDZ = Fetch.Post("JYDZ");
                if (string.IsNullOrEmpty(JYDZ))
                {
                    isSuccess = false;
                    errorMessage = "经营地址必须填写";
                }else
                {
                    infoModel.JYDZ = JYDZ;

                }
             

                string FDDBRMC = Fetch.Post("FDDBRMC");
                if (string.IsNullOrEmpty(FDDBRMC))
                {
                    isSuccess = false;
                    errorMessage = "法人代表不能为空";
                } 
                else
                {
                    infoModel.FDDBRMC = FDDBRMC;
                }
                string FRDHHM = Fetch.Post("FRDHHM");
                if (string.IsNullOrEmpty(FRDHHM))
                {
                    isSuccess = false;
                    errorMessage = "法人代表电话不能为空";
                } 
                else
                {
                    infoModel.FRDHHM = Fetch.Post("FRDHHM");
                }
               
                infoModel.Nsrsbh = Fetch.Post("Nsrsbh");
                infoModel.Nsrsbh_DS = Fetch.Post("Nsrsbh_DS");
                string TJRMC=Fetch.Post("TJRMC");
                if(string.IsNullOrEmpty(TJRMC))
                {
                      isSuccess = false;
                      errorMessage = "统计人名称必须填写";

                }else
                {
                    infoModel.TJRMC = TJRMC;

                }
                string TjrDH=Fetch.Post("TjrDH");
                if (string.IsNullOrEmpty(TjrDH))
                {
                     isSuccess = false;
                      errorMessage = "统计人电话必须填写";
                } 
                else
                {
                    infoModel.TjrDH = TjrDH;
                }
              
                int Cyrsval = 0;
                string Cyrs = Fetch.Post("Cyrs");
                if (!string.IsNullOrEmpty(Cyrs))
                {
                    if (int.TryParse(Cyrs, out Cyrsval))
                    {

                        infoModel.Cyrs = Cyrsval;
                    }
                    else
                    {

                        isSuccess = false;
                        errorMessage = "从业人数填写错误";
                    }

                }
                string CYRS_BK = Fetch.Post("CYRS_BK");


                int CYRS_BKval = 0;
                if (!string.IsNullOrEmpty(CYRS_BK))
                {


                    if (int.TryParse(CYRS_BK, out CYRS_BKval))
                    {
                        if (Cyrsval != 0 & Cyrsval > CYRS_BKval)
                        {
                            infoModel.CYRS_BK = CYRS_BKval;
                        }
                        else
                        {
                            isSuccess = false;
                            errorMessage = "从业人数其中本科填写错误";

                        }
                    }

                }
                string DXHM = Fetch.Post("DXHM");
                string pattern = @"\d+";
                if (string.IsNullOrEmpty(DXHM))
                {

                    isSuccess = false;
                    errorMessage = "短信号码不能为空";

                }
                else
                {
                    if (Regex.IsMatch(DXHM, pattern))
                    {
                        infoModel.DXHM = DXHM;


                    }
                    else
                    {
                        isSuccess = false;
                        errorMessage = "短信号码填写错误";

                    }
                }


                // infoModel.q = Fetch.Post("QQHM");
                string pt = @"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?";
                string Email = Fetch.Post("Email");

                if (!string.IsNullOrEmpty(Email))
                {

                    if (Regex.IsMatch(Email, pt))
                    {




                        infoModel.Email = Email;
                    }
                    else
                    {
                        isSuccess = false;
                        errorMessage = "Email填写错误";

                    }

                }

                string FZRMC = Fetch.Post("FZRMC");

                if (string.IsNullOrEmpty(FZRMC))
                {
                    isSuccess = false;
                    errorMessage = "联系人必须填写";
                    
                } 
                else
                {
                    infoModel.FZRMC = FZRMC;
                }
                string FZRDH = Fetch.Post("FZRDH");

                if (string.IsNullOrEmpty(FZRDH))
                {
                    isSuccess = false;
                    errorMessage = "联系人电话必须填写";
                } 
                else
                {
                    infoModel.FZRDH = FZRDH;
                }
                string QYDL_DM = Fetch.Post("QYDL_DM");

                if (string.IsNullOrEmpty(QYDL_DM))
                {

                    isSuccess = false;
                    errorMessage = "企业大类必须填写";

                }
                else
                {


                    infoModel.QYDL_DM = QYDL_DM;
                }

                infoModel.QYLX_DM = Fetch.Post("QYLX_DM");
                infoModel.QQHM = Fetch.Post("QQHM");
                infoModel.QYLXMX_DM = Fetch.Post("hdQYLXMX");
                infoModel.SDYSX1_DM = Fetch.Post("SDYSX1_DM");
                infoModel.SDYSX2_DM = Fetch.Post("SDYSX2_DM");

                infoModel.SDYSX3_DM = Fetch.Post("SDYSX3_DM");
                infoModel.SDYSX4_DM = Fetch.Post("SDYSX4_DM");
                infoModel.SDYSX5_DM = Fetch.Post("SDYSX5_DM");
                infoModel.QyGH = Fetch.Post("QyGH");
                if (isSuccess)
                {
                    switch (Fetch.Post("hdMethod"))
                    {


                        case BaseStruct.ActionOperationStruct.ADD_NEXT:
                            {
                                infoModel.ZTDM = "0";
                                DJ_QYZCXXBLL.Add(infoModel);

                                errorMessage = "操作成功";

                                break;
                            }


                        case BaseStruct.ActionOperationStruct.EDIT:
                            {
                                infoModel.ZTDM = "0";
                                DJ_QYZCXXBLL.Update(infoModel);

                                errorMessage = "操作成功";
                                break;
                            }

                    }

                }
            }
            catch (System.Exception ex)
            {
                errorMessage = ex.Message;
            }
            if (string.IsNullOrEmpty(errorMessage))
            {
                errorMessage = "操作失败";

            }
            return errorMessage;
        }
    }
}
